package com.hsd.ha.app.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hsd.app.model.HsdDssinfo;
import com.hsd.commons.model.HsdAccount;
import com.hsd.core.base.action.BaseAction;
import com.hsd.core.cache.DictCache;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.core.utils.TimeUtil;
import com.hsd.ha.app.model.HaSurveyInst;
import com.hsd.ha.app.service.IHealthAssessmentService;
import com.hsd.ha.app.vo.PhyVo;



@Controller
@RequestMapping("/ha/phy")
public class PhysiqueHaAction extends BaseAction{
	@Autowired
	private IHealthAssessmentService healthAssessmentService;
	 
	public void setHealthAssessmentService(
			IHealthAssessmentService healthAssessmentService) {
		this.healthAssessmentService = healthAssessmentService;
	}
	/**
	 * 健康测评 ：体质测试首页
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadPhyIndex.do")
	public String loadPhyIndex(PageParams pageParams,HttpServletRequest request,ModelMap model){
		
		return "app/evaluation/phy/phy-index";
	} 
	/**
	 * 健康测评 ：体质测试内容页
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadPhyContent.do")
	public String loadPhyContent(PageParams pageParams,HttpServletRequest request,ModelMap model){
		System.out.println("=====content=================");
		return "app/evaluation/phy/phy-content";
	} 
	/**
	 * 健康测评 ：体质测评计算
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 * @throws IOException 
	 */
 	@RequestMapping(value="/loadPhyCaculation.do", method = RequestMethod.POST)
	public  void loadPhyCaculation(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
 		String valuestr = request.getParameter("valuestr");
 		String idstr = request.getParameter("idstr");
 		HsdAccount accinfo = this.getCurrentuser(request);
 		HaSurveyInst surveyInst = new HaSurveyInst();
 		Long accid = 0l; //游客
 		String username = "游客";
 		Long usersex = 0l;//未知
 		String userage = "-1";//未知
 		if(null != accinfo){
	 			accid = accinfo.getId();  
	 	 		username = accinfo.getUsername();
	 	 		usersex = accinfo.getUsersex(); 
	 	 		userage = accinfo.getUseage(); 
 		}
 		surveyInst.setAccid(accid);
 		surveyInst.setUsername(username);
 		surveyInst.setSurveydetail(idstr);
 		surveyInst.setAgenum(userage);
 		surveyInst.setUsersex(usersex);
 		surveyInst.setSurveyresult(valuestr);
 		surveyInst.setSurveystatus(1l);
 		surveyInst.setSurveyid(2l); 
 		surveyInst.setSurveydate(TimeUtil.getCurrentDateTime());
 		this.healthAssessmentService.save(surveyInst); 
 		System.out.println("=====valuestr=================:" + valuestr);
 		PhyVo phyObj = this.healthAssessmentService.calculatePhy(valuestr, null);
// 		//"yangxuzhi:50|yinxuzhi:50|qixuzhi:50|tanshizhi:50|shirezhi:50|xuetanzhi:50|tebingzhi:50|qiyuzhi:50|pinghezhi:50"
 		System.out.println("valuestr:" + valuestr);
 		System.out.println("main:" + phyObj.getMain_phy());
 		System.out.println("both:" + phyObj.getBoth_phy());
 		System.out.println("forward:" + phyObj.getForward_phy());
 		System.out.println("phyObj:" + phyObj.toString());
 		
 		PrintWriter out = response.getWriter();
	      out.print(phyObj.toString());
	      out.flush();
	      out.close(); 
	} 
 
	/**
	 * 健康测评 ：体质测试结果
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
 	@RequestMapping(value="/loadPhyEvaResult.do")
	public   String loadPhyEvaResult(HttpServletRequest request,ModelMap model){
 		String idstr = request.getParameter("idstr");
 		String valuestr = request.getParameter("valuestr");
// 		HsdAccount accinfo = this.getCurrentuser(request);
// 		HaSurveyInst surveyInst = new HaSurveyInst();
// 		Long accid = 0l; //游客
// 		String username = "游客";
// 		Long usersex = 0l;//未知
// 		String userage = "-1";//未知
// 		if(null != accinfo){
//	 			accid = accinfo.getId();  
//	 	 		username = accinfo.getUsername();
//	 	 		usersex = accinfo.getUsersex(); 
//	 	 		userage = accinfo.getUseage(); 
// 		}
// 		surveyInst.setAccid(accid);
// 		surveyInst.setUsername(username);
// 		surveyInst.setSurveydetail(idstr);
// 		surveyInst.setAgenum(userage);
// 		surveyInst.setUsersex(usersex);
// 		surveyInst.setSurveyresult(valuestr);
// 		surveyInst.setSurveystatus(1l);
// 		surveyInst.setSurveyid(2l); 
// 		surveyInst.setSurveydate(TimeUtil.getCurrentDateTime());
// 		this.healthAssessmentService.save(surveyInst); 
		return "app/evaluation/phy/phy-result";
	} 
 	/**
	 * 健康测评 ：体质养生建议
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 * @throws IOException 
	 */
 	@RequestMapping(value="/loadPhyAdvise.do", method = RequestMethod.POST)
	public  void loadPhyAdvise(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
 		StringBuilder builderStr = new StringBuilder();
 		DictCache dictCache = DictCache.getInstance();
 		

 		String phycode = request.getParameter("phycode"); 
 		Map query = new HashMap();
		query.put("codestr", phycode);
		query.put("youxiaobj", new Long(1));
		Object existData = this.healthAssessmentService.getUniqueObject(HsdDssinfo.class, query); 
		if(existData != null){
			HsdDssinfo dsinfo = (HsdDssinfo)existData;
			String dssname = dsinfo.getDssname();
	 		builderStr.append("何为<font size=3><strong>").append(dssname).append("</strong></font><br>");
	 		builderStr.append(dsinfo.getDesccontent()).append("\r\n");
	 		 
	 		//获取该类体质人群的调养方案
	 		
	 		List hplist = this.healthAssessmentService.loadHpListbyCode(dsinfo.getCodestr());
	 		if(hplist.size() > 0 ){
	 			builderStr.append("<font size=3><strong>").append(dssname).append("</strong></font>的养生方案<br>");
		 		for(int i = 0;i<hplist.size();i++){
		 			Map data = (Map)hplist.get(i);
		 			String type = data.get("type")+"";
		 			String typevalue = dictCache.getCodeName("[调养方案类别]",type,null);
		 			String hpcontent = (String)data.get("hpcontent");
		 			builderStr.append(i+1).append(". ").append(typevalue).append("<br>");
		 			builderStr.append(hpcontent).append("<br>");
		 		}
	 		}
		}
	   response.setCharacterEncoding("UTF-8");
	   PrintWriter out = response.getWriter();
       out.print(builderStr.toString());
       out.flush();
       out.close(); 
	} 
 	/**
	 * 健康测评 ：体质测评信息列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadPhydataList.do")
	public String loadPhydataList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String username = request.getParameter("username"); 
		String startdate = request.getParameter("startdate"); 
		String enddate = request.getParameter("enddate");  
		Map paramMap = new HashMap();
		paramMap.put("username", username);
		paramMap.put("startdate", startdate);
		paramMap.put("enddate", enddate);
		Pagination page = this.healthAssessmentService.loadEvaDataList(pageParams,paramMap);
		
		model.addAttribute("page",page);
		model.addAttribute("username",username); 
		model.addAttribute("startdate",startdate); 
		model.addAttribute("enddate",enddate); 
		return "app/evaluation/phy/phy-data-list";
	} 
	
}
